package com.jeff.android.lab.module.anim.path;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * @author: duff
 * @date: 2019/1/3
 * @since: 1.0.0
 */
public class WaveView extends View implements Wave {
    private Paint mPaint;
    private Path mPath;

    private int mWidth = 0;
    private int mHeight = 0;

    private int mWaveLength = 0; // 波浪宽度
    private int mOffset = 0; //
    private int mWaveHeight = 0; // 波浪距底部高度
    private int mWaveMaxHeight = 0;
    private int mPeakHeight = 0; // 波峰、波谷高度
    private int mWaveCount = 0; // 波浪数

    private ValueAnimator mValueAnimator;

    public WaveView(Context context) {
        super(context);
        init();
    }

    public WaveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#00aff9"));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(5);

        mPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;

        mPeakHeight = 150;
        mWaveHeight = mHeight - mPeakHeight;
        mWaveLength = mWidth;
        mWaveCount = Math.round(mWidth / mWaveLength) + 1;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawWave(canvas);
    }

    private void drawWave(Canvas canvas) {
        mPath.reset();

        mPath.moveTo(-mWaveLength + mOffset, mWaveHeight);

        for (int i = 0; i < mWaveCount; i++) {
            mPath.rQuadTo(mWaveLength / 4, -mPeakHeight, mWaveLength / 2, 0);
            mPath.rQuadTo(mWaveLength / 4, mPeakHeight, mWaveLength / 2, 0);
        }

        mPath.lineTo(getWidth(), getHeight());
        mPath.lineTo(0, getHeight());
        mPath.close();

        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public void start() {
        startAnim();
    }

    @Override
    public void end() {
        if (mValueAnimator != null &&
                mValueAnimator.isRunning()) {
            mValueAnimator.end();
        }
    }

    @Override
    public void setPercent(float percent) {
        if (percent == 1.0f) {
            mWaveMaxHeight = mHeight;
        } else {
            mWaveMaxHeight = (int) (mHeight * (1 - percent));
        }
    }

    public void setPeakHeight(int height) {
        mPeakHeight = height;
    }


    private void startAnim() {
        mValueAnimator = ValueAnimator.ofInt(0, mWaveLength);
        mValueAnimator.setDuration(1000);
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimator.setInterpolator(new LinearInterpolator());
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mOffset = (int) animation.getAnimatedValue();
                if (mWaveHeight > mWaveMaxHeight) {
                    mWaveHeight--;
                }
                postInvalidate();
            }
        });

        mValueAnimator.start();
    }


}
